// pages/payment/payment.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    height: app.globalData.navBarHeight + app.globalData.statusBarHeight,
    statusBarHeight: app.globalData.statusBarHeight,
    navBarHeight: app.globalData.navBarHeight,
    spaceLineList: [1,2,3,4,5,6,7,8,9,10,11,12,13,14],
    goodsInfoList: [],
    totalCount: 0,
    totalPrice: "0.00",
    totalIntPrice: '0',
    totalFloatPrice: '00'
  },

  getCartSelectedList() {
    wx.request({
      url: 'http://localhost:8082/app/omsGoodsCart/getSelectedCart',
      method: "GET",
      data: {},
      success: (res) => {
        res = res.data;
        if (res.code != 200) {
          return;
        }
        let data = res.data;
        let list = data;
        let totalSelected = true;
        list = list.map(item => {
          let goodsInfo = item.goodsInfo || {};
          let actualPrice = goodsInfo.actualPrice + "";
          return {
            ...item,
            name: goodsInfo.name,
            coverUrl: goodsInfo.coverUrl,
            actualPrice: actualPrice,
            actualIntPrice: actualPrice.substring(0, actualPrice.length - 2),
            actualFloatPrice: actualPrice.substring(actualPrice.length - 2)
          }
        })

        this.calcTotalPrice(list.filter(item => item.selected));
        this.setData({
          goodsInfoList: list,
          totalSelected
        })
      },
      fail: (err) => {
        console.error("2222", err)
      }
    })
  },

  /***
   * 生成订单
   */
  toGenerateOrderAndPay(){
      let goodsInfoList = this.data.goodsInfoList || [];
      console.log(goodsInfoList)
      let formData = {
        useCoupon: 0,
        couponIds: [],
        orderGoodsInfoList: [
          ...goodsInfoList.map(item=>{
            return {
              productId: item.goodsInfo && item.goodsInfo.id,
              productAmount: item.amount
            }
          })
        ]
      }
      wx.request({
        url: 'http://localhost:8082/app/omsOrderInfo',
        method: "POST",
        data: {
          ...formData
        },
        success: (res) => {
          res = res.data;
          if (res.code != 200) {
            return;
          }
          let data = res.data;
          console.log("toGenerateOrderAndPay: ",data);
          wx.navigateTo({
            url: '/pages/order/order',
          })
        },
        fail: (err) => {
          wx.showToast({
            title: err && err.msg || err || "网络错误",
          })
          console.error("2222", err)
        }
      })
  },

  calcTotalPrice(selectedCartList = []) {
    let totalPrice = 0;
    let totalCount = 0;
    selectedCartList.forEach(item => {
      item.selected = true;
      //计算总价格
      totalCount += item.amount;
      totalPrice += item.actualPrice * item.amount;
    });
    let strTotlePrice = totalPrice + '';
    let totalIntPrice = strTotlePrice.substring(0, strTotlePrice.length - 2);
    let totalFloatPrice = strTotlePrice.substring(strTotlePrice.length - 2);
    this.setData({
      totalCount,
      totalIntPrice: totalPrice == 0 ? '0' : `${totalIntPrice}`,
      totalFloatPrice: totalPrice == 0 ? '00' : `${totalFloatPrice}`,
      totalPrice: totalPrice == 0 ? '0.00' : `${totalIntPrice}.${totalFloatPrice}`
    })
  },


  toBack(){
    wx.navigateBack();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getCartSelectedList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})